<?php
/*
 * @Author: CYH
 * @Date: 2020-10-31 10:25:06
 * @LastEditTime: 2021-05-08 17:58:08
 * @LastEditors: Please set LastEditors
 * @Description: 房间管理model类
 * @FilePath: \application\models\Room\Room_manage.php
 */
    //房间管理model类
    class Room_manage extends CI_Model
    {
        //构造方法
        public function __construct()
        {

            parent::__construct();
            $this->load->database();
        }

        /**
         * @description: 房间管理新增方法 
         * @param {area_number, floor_number, dorm_floor, room_number, room_type, room_sex, liveNumber, cabinet_number, room_status, temporary, cost, costman}
         * @return {*}
         */        
        public function room_insert($area_number, $floor_number, $dorm_floor, $room_number, $room_type, $room_sex, $liveNumber, $room_status, $temporary, $cost, $costman)
        {

            //若必填值为空则报错
            if ($area_number == '' || $floor_number == '' || $dorm_floor == '' || $room_number == '') 
            {
                $return = array('res' => '40002', 'msg' => '请输入必填字段' ,'data' => '');
                $result = json_encode($return);
            } 
            else 
            {
                $data_bed_all = [];
                $query = $this->db->get('dorm_sslmanage' ,array('area_number' => $area_number, 'floor_number' => $floor_number));
                
                //更新房间信息表
                $data_roominfor = array(
                    'area_number' => $area_number,              //区域编号
                    'area_name' => $query->row()->area_name,    //区域名称
                    'floor_number' => $floor_number,            //宿舍楼编号
                    'floor_name' => $query->row()->floor_name,  //宿舍楼名称
                    'dorm_floor' => $dorm_floor,                //宿舍所在楼层
                    'room_number' => $room_number,              //宿舍房间号
                    'room_type' => $room_type,                  //宿舍类型
                    'room_sex' => $room_sex,                    //男/女宿舍
                    'liveNumber' => $liveNumber,                //宿舍可住人数
                    'remainNumber' => $liveNumber,              //宿舍剩余可住人数
                    'room_status' => $room_status,              //房间状态
                    'temporary' => $temporary,                  //是否临时
                    // 'cabinet_number' => $cabinet_number,     //柜子数量
                    'cost' => $cost,                            //住宿费用
                    'costman' => $costman                       //住宿费用承担人
                );
                $this->db->insert('dorm_roominfor', $data_roominfor);
                $return_roominfor = $this->db->affected_rows();

                //更新床位表
                for($i=1; $i<=$liveNumber; $i++)
                {
                    $data_bed = array(
                        'area_number' => $area_number,              //区域编号
                        'floor_number' => $floor_number,            //宿舍楼编号
                        'dorm_floor' => $dorm_floor,                //宿舍所在楼层
                        'room_number' => $room_number,              //宿舍房间号
                        'bedStatus' => '1',                         //床位是住人
                        'check_inWhether' => '0',                   //床位是否住人
                        'bedNumber' => $i                           //床位号
                    );

                    array_push($data_bed_all, $data_bed);
                }
                $this->db->insert_batch('dorm_bed', $data_bed_all);
                $return_bed = $this->db->affected_rows();


                if ($return_roominfor && $return_bed) 
                {
                    //新增成功
                    $return_su = array('res' => '40000', 'msg' => '新增成功' ,'data' => '');
                    $result = json_encode($return_su);
                } 
                else 
                {
                    //新增失败
                    $return_fa = array('res' => '40001', 'msg' => '新增失败' ,'data' => '');
                    $result = json_encode($return_fa);
                }
            }
            return $result;
        }

        /**
         * @description: 房间管理删除方法
         * @param {area_number, floor_number, room_number}
         * @return {*}
         */        
        public function room_delete($area_number, $floor_number, $room_number)
        {
            //按区域编号和宿舍楼编号进行取值
            $query = $this->db->get_where('dorm_roominfor' ,array('area_number' => $area_number ,'floor_number' => $floor_number ,'room_number' => $room_number));
            //如果宿舍实际人数不为零，则不允许删除
            if ($query->row()->peopleNumber != 0) {
                $return = array('res' => '40001', 'msg' => '居住人数不为空不允许删除' ,'data' => '');
                $result = json_encode($return);
            //若宿舍实际人数为零则允许删除
            } 
            else 
            {  
                $this->db->delete('dorm_roominfor', array('area_number' => $area_number ,'floor_number' => $floor_number ,'room_number' => $room_number));
                $return = $this->db->affected_rows();
                if ($return) 
                {
                    //删除成功
                    $return_su = array('res' => '40000', 'msg' => '删除成功' ,'data' => '');
                    $result = json_encode($return_su);
                }
                else 
                {
                    //删除失败
                    $return_fa = array('res' => '40001', 'msg' => '删除失败' ,'data' => '');
                    $result = json_encode($return_fa);
                }
            }
            return $result;
        }

        /**
         * @description: 房间管理修改方法
         * @param {area_number, floor_number, room_number, roomNumber_after, dorm_floor, room_type, room_status, temporary, room_sex, costman, liveNumber, cost}
         * @return {*}
         */        
        public function room_update($area_number, $floor_number, $room_number, $roomNumber_after, $dorm_floor, $room_type, $room_status, $temporary, $room_sex, $costman, $liveNumber, $cost)
        {
            //若必输字段为空则报错
            if ($area_number == '' || $floor_number == '' || $room_number == '') 
            {
                $return = array('res' => '40002', 'msg' => '请输入必填字段' ,'data' => '');
                $result = json_encode($return);
            }
            else 
            {

                if($roomNumber_after != FALSE)
                {
                    //更新房间信息表
                    $data_room = array(
                    // 'area_number' => $area_number,           //区域编号
                    // 'floor_number' => $floor_number,         //宿舍楼编号    
                    'room_number' => $roomNumber_after,         //更改后宿舍房间号
                    // 'dorm_floor' => $dorm_floor,             //宿舍楼层号
                    'room_type' => $room_type,                  //宿舍类型
                    'room_status' => $room_status,              //房间状态
                    'temporary' => $temporary,                  //是否临时
                    'room_sex' => $room_sex,                    //男/女宿舍
                    'costman' => $costman,                      //住宿费用承担人
                    'liveNumber' => $liveNumber,                //宿舍可住人数
                    'cost' => $cost,                            //住宿费用 
                    );
                }
                else 
                {
                    //更新房间信息表
                    $data_room = array(
                        // 'area_number' => $area_number,           //区域编号
                        // 'floor_number' => $floor_number,         //宿舍楼编号    
                        // 'room_number' => $roomNumber_after,      //更改后宿舍房间号
                        // 'dorm_floor' => $dorm_floor,             //宿舍楼层号
                        'room_type' => $room_type,                  //宿舍类型
                        'room_status' => $room_status,              //房间状态
                        'temporary' => $temporary,                  //是否临时
                        'room_sex' => $room_sex,                    //男/女宿舍
                        'costman' => $costman,                      //住宿费用承担人
                        'liveNumber' => $liveNumber,                //宿舍可住人数
                        'cost' => $cost,                            //住宿费用 
                        );
                }

                $where = array('area_number' => $area_number, 'floor_number' => $floor_number, 'room_number' => $room_number);
                $this->db->where($where);
                $this->db->update('dorm_roominfor', $data_room);
                $query_room = $this->db->affected_rows();

                //取房间床位信息
                $query_bed = $this->db->get_where('dorm_bed', array('area_number' => $area_number, 'floor_number' => $floor_number, 'dorm_floor' => $dorm_floor, 'room_number' => $room_number));

                if($query_bed->num_rows() > 0 && $roomNumber_after != FALSE)
                {
                    //更新床位信息表
                    $data_bed = array(
                        'room_number' => $roomNumber_after //更改后宿舍房间号
                    );
                    $where = array('area_number' => $area_number, 'floor_number' => $floor_number, 'room_number' => $room_number);
                    $this->db->where($where);
                    $this->db->update('dorm_bed', $data_bed);
                    $query_bed = $this->db->affected_rows();
                }
                else 
                {
                    $query_bed = true;
                }
                
                //更新入住人信息表
                $query_livepeople = $this->db->get_where('dorm_livepeople', array('area_number' => $area_number, 'floor_number' => $floor_number, 'room_number' => $room_number));
                if($query_livepeople->num_rows()>0 && $roomNumber_after != FALSE)
                {
                    $data_livepeople = array(
                        'room_number' => $roomNumber_after //更改后宿舍房间号
                    );
                    $where = array('area_number' => $area_number, 'floor_number' => $floor_number, 'room_number' => $room_number);
                    $this->db->where($where);
                    $this->db->update('dorm_livepeople', $data_livepeople);
                    $query_livepeople = $this->db->affected_rows();
                }
                else 
                {
                    $query_livepeople = true;
                }

                if ($query_room && $query_bed && $query_livepeople) 
                {
                    //修改成功
                    $return_su = array('res' => '40000', 'msg' => '修改成功', 'data' => '');
                    $result = json_encode($return_su);
                } 
                else 
                {
                    //修改失败
                    $return_fa = array('res' => '40001', 'msg' => '修改失败' ,'data' => '');
                    $result = json_encode($return_fa);
                }
            }
            return $result;
        }

        /**
         * @description: 房间管理查询方法-总条数
         * @param {area_number, floor_number, room_sex, room_number, liveNumber, remainNumber}
         * @return {*}
         */        
        public function room_select_all($area_number, $floor_number, $room_sex, $room_number, $liveNumber, $remainNumber)
        {
            //拼接sql语句
            $sj1 = $area_number == '' ? ' 1 = 1 ' : " area_number LIKE '%".$area_number."%'";               //区域编号
            $sj2 = $floor_number == '' ? ' AND 1 = 1 ' : " AND floor_number LIKE '%".$floor_number."%'";    //宿舍楼编号
            $sj3 = $room_sex == '' ? ' AND 1 = 1 ' : " AND room_sex LIKE '%".$room_sex."%'";                //男/女宿舍
            $sj4 = $room_number == '' ? ' AND 1 = 1 ' : " AND room_number = '".$room_number."'";       //宿舍房间号
            $sj5 = $liveNumber == '' ? ' AND 1 = 1 ' : " AND liveNumber = '".$liveNumber."'";               //宿舍可住人数
            $sj6 = $remainNumber == '' ? ' AND 1 = 1 ' : " AND remainNumber = '".$remainNumber."'";         //宿舍剩余可住人数
            $sql = 'SELECT * FROM dorm_roominfor WHERE' .$sj1 .$sj2 .$sj3 .$sj4 .$sj5 .$sj6;
            $query = $this->db->query($sql, $sj1, $sj2, $sj3, $sj4 ,$sj5 ,$sj6);
            $all = $query->num_rows();
            return $all;
        }

        /**
         * @description: 房间管理查询方法
         * @param {area_number, floor_number, room_sex, room_number, liveNumber, remainNumber, limit, count}
         * @return {*}
         */        
        public function room_select($area_number, $floor_number, $room_sex, $room_number, $liveNumber, $remainNumber, $limit, $count)
        {
            //拼接sql语句
            if($limit != '')
            {
                $limit1 = ("$limit" - 1) * 10;
            }
            $sj1 = $area_number == '' ? ' 1 = 1 ' : " area_number LIKE '%".$area_number."%'";               //区域编号
            $sj2 = $floor_number == '' ? ' AND 1 = 1 ' : " AND floor_number LIKE '%".$floor_number."%'";    //宿舍楼编号
            $sj3 = $room_sex == '' ? ' AND 1 = 1 ' : " AND room_sex LIKE '%".$room_sex."%'";                //男/女宿舍
            $sj4 = $room_number == '' ? ' AND 1 = 1 ' : " AND room_number = '".$room_number."'";       //宿舍房间号
            $sj5 = $liveNumber == '' ? ' AND 1 = 1 ' : " AND liveNumber = '".$liveNumber."'";               //宿舍可住人数
            $sj6 = $remainNumber == '' ? ' AND 1 = 1 ' : " AND remainNumber = '".$remainNumber."'";         //宿舍剩余可住人数
            $sj7 = $limit == '' ? ' AND 1 = 1 ' : "Limit " . $limit1 . ",10";                               //页数
            if($limit != '')
            {
                $sql = "SELECT * FROM dorm_roominfor WHERE" .$sj1 .$sj2 .$sj3 .$sj4 .$sj5 .$sj6 .$sj7;
                $query = $this->db->query($sql, $sj1, $sj2, $sj3, $sj4 ,$sj5 ,$sj6 ,$sj7);
            } 
            else 
            {
                $sql = "SELECT * FROM dorm_roominfor WHERE" .$sj1 .$sj2 .$sj3 .$sj4 .$sj5 .$sj6;
                $query = $this->db->query($sql, $sj1, $sj2, $sj3, $sj4 ,$sj5 ,$sj6);
            }
            if ($query->num_rows() > 0) 
            {
                $return_su = array('res' => '40000', 'msg' => '查询成功', 'data' => $query->result() ,'page' => $count);
                $result = json_encode($return_su);
            } 
            else 
            {
                $return_fa = array('res' => '40001', 'msg' => '' ,'data' => '');
                $result = json_encode($return_fa);
            }
            return $result;
        }

        /**
         * @description: 房间初始化登陆方法
         * @param {initPassword}
         * @return {*}
         */        
        public function room_initialization($initPassword)
        {
            //必输值为空报错
            if ($initPassword == '') 
            {
                $return = array('res' => '40002', 'msg' => '请输入密码' ,'data' => '');
                $result = json_encode($return);
            } 
            else 
            {
                $query = $this->db->get('dorm_inpassword');
                //判断密码
                if ($initPassword != $query->row()->initPassword) 
                {
                    //密码错误
                    $return_fa = array('res' => '40001', 'msg' => '密码错误', 'data' => '');
                    $result = json_encode($return_fa);
                } 
                else 
                {
                    //密码正确，查询成功
                    $return_su = array('res' => '40000', 'msg' => '查询成功', 'data' => $query->row()->initPassword);
                    $result = json_encode($return_su);
                }
            }
            return $result;
        }

        /**
         * @description: 房间初始化表单方法
         * @param {area_number, floor_number, floor1, floor2, room_type, room_sex, room_Prefix, liveNumber, cabinet_number, cost, costman, room_status, temporary, roomNumber}
         * @return {*}
         */        
        public function room_form($area_number, $floor_number, $floor1, $floor2, $room_type, $room_sex, $room_Prefix, $liveNumber, $cabinet_number, $cost, $costman, $room_status, $temporary, $roomNumber)
        {
            //取更新房间信息的数据
            $where = array('area_number' => $area_number, 'floor_number' => $floor_number);
            $this->db->where($where);
            $where1 = array('floor_number' => $floor1, 'floor_number' => $floor2);
            $this->db->or_where($where1);
            $this->db->delete('dorm_roominfor');
            $select = array('area_name', 'floor_name');
            $this->db->select($select);
            $this->db->where($where);
            $query = $this->db->get('dorm_sslmanage');

            //取更新宿舍信息的数据
            $data = array(
                'liveNumber' => 0
            );
        
            if ($floor2 >= $floor1) 
            {
                for ($floor_now = $floor1; $floor_now <= $floor2; $floor_now++) 
                {
                    // $where2 = array('area_number' => $area_number, 'floor_number' => $floor_number, 'dorm_floor' => $floor_now);
                    $this->db->delete('dorm_bed', array('area_number' => $area_number, 'floor_number' => $floor_number, 'dorm_floor' => $floor_now));
                    for ($room_number_now = 1; $room_number_now <= $roomNumber; $room_number_now++) 
                    {
                        if ($room_number_now < 10) 
                        {
                            $room_number = $floor_now .'0' .$room_number_now;
                        } 
                        else 
                        {
                            $room_number = $floor_now . $room_number_now;
                        }
                        $data1 = array(
                            'area_number' => $area_number,              //区域编号
                            'area_name' => $query->row()->area_name,    //区域名称
                            'floor_number' => $floor_number,            //宿舍楼编号
                            'floor_name' => $query->row()->floor_name,  //宿舍楼名称
                            'dorm_floor' => $floor_now,                 //宿舍楼层号
                            'room_number' => $room_number,              //宿舍房间号
                            'room_type' => $room_type,                  //房间类型
                            'room_sex' => $room_sex,                    //男/女宿舍
                            'room_Prefix' => $room_Prefix,              //房间号前缀
                            'liveNumber' => $liveNumber,                //宿舍可住人数
                            'cabinet_number' => $cabinet_number,        //柜子数量
                            'cost' => $cost,                            //住宿费用
                            'costman' => $costman,                      //住宿费用承担人
                            'room_status' => $room_status,              //住宿费用承担人
                            'temporary' => $temporary                   //是否临时
                        );
                        $this->db->insert('dorm_roominfor', $data1);
                        $return = $this->db->affected_rows();

                        for ($bednumber_now = 1; $bednumber_now <= $liveNumber; $bednumber_now++) 
                        {
                            $data2 = array(
                                'area_number' => $area_number,          //区域编号
                                'floor_number' => $floor_number,        //宿舍楼编号
                                'dorm_floor' => $floor_now,             //宿舍楼层号    
                                'room_number' => $room_number,          //宿舍房间号
                                'bedStatus' => 1,                       //床位状态
                                'check_inWhether' => 0,                 //床位是否住人
                                'bedNumber' => $bednumber_now,          //床位号
                            );
                            $this->db->insert('dorm_bed', $data2);
                        }
                    }
                    //更新宿舍信息
                    $where4 = array('area_number' => $area_number, 'floor_number' => $floor_number);
                    $this->db->where($where4);
                    $this->db->update('dorm_sslmanage', $data);
                }
            } 
            else 
            {
                $return1 = array('res' => '40001', 'msg' => '上限比下限大' ,'data' => '');
                $result = json_encode($return1);
            }
            if ($return) 
            {
                //房间初始化成功
                $return_su = array('res' => '40000', 'msg' => '房间初始化成功' ,'data' => '');
                $result = json_encode($return_su);
            } 
            else 
            {
                //房间初始化失败
                $return_fa = array('res' => '40001', 'msg' => '房间初始化失败' ,'data' => '');
                $result = json_encode($return_fa);
            }
            return $result;
            }

        /**
         * @description:房间信息excel导出查询 
         * @param {*}
         * @return {*}
         */    
        public function excel_room()
        {
            $query = $this->db->query("SELECT area_name ,floor_name ,caveNumber ,dorm_floor ,room_number ,room_type ,room_status ,temporary ,room_sex ,costman ,liveNumber ,peopleNumber ,cost FROM dorm_roominfor ORDER BY area_name ,floor_name ,dorm_floor ,room_number");
            $result = $query->result();
            return json_encode(array('ret' => '40001', 'msg' => $result));
        }

        /**
         * @description:取当前层非公共房间 
         * @param {area_number, floor_number, dorm_floor}
         * @return {*}
         */
        public function depublicRoom($area_number, $floor_number, $dorm_floor)
        {
            $room = $this->db->get_where('dorm_roominfor', array('area_number' => $area_number, 'floor_number' => $floor_number, 'dorm_floor' => $dorm_floor, 'costman' => 0) );
            if($room->num_rows()>0)
            {
                return $room->result();
            }
            else
            {
                return 0;
            }

        }

        /**
         * @description:取每层的有人入住的所有房间 
         * @param {area_number, floor_number, dorm_floor}
         * @return {*}
         */
        public function floorRoom($area_number, $floor_number, $dorm_floor)
        {
            $query = $this->db->get_where('dorm_roominfor', array('area_number' => $area_number, 'floor_number' => $floor_number, 'dorm_floor' => $dorm_floor, 'peopleNumber >' => 0));
            if($query)
            {
                return json_encode(array('res' => '40000', 'msg' => '查询成功', 'data' => $query->result()));
            }
            else
            {
                return json_encode(array('res' => '40001', 'msg' => '查询无数据', 'data' => ''));
            }
        }

        //取整栋宿舍楼有人入住的所有房间
        public function dorm_room($area_number, $floor_number)
        {
            $query = $this->db->get_where('dorm_roominfor', array('area_number' => $area_number, 'floor_number' => $floor_number, 'peopleNumber >' => 0));
            if($query)
            {
                return json_encode(array('res' => '40000', 'msg' => '查询成功', 'data' => $query->result()));
            }
            else
            {
                return json_encode(array('res' => '40001', 'msg' => '查询无数据', 'data' => ''));
            }
        }

        /**
         * @description: 取每个宿舍楼的有人入住的所有房间
         * @param {area_number, floor_number}
         * @return {*}
         */
        public function dormRoom($area_number, $floor_number)
        {
            $query = $this->db->get_where('dorm_roominfor', array('area_number' => $area_number, 'floor_number' => $floor_number, 'peopleNumber >' => 0));
            if($query)
            {
                return json_encode(array('res' => '40000', 'msg' => '查询成功', 'data' => $query->result()));
            }
            else
            {
                return json_encode(array('res' => '40001', 'msg' => '查询无数据', 'data' => ''));
            }

        }

        /**
         * @description: 取房间人员
         * @param {area_number, floor_number, dorm_floor, room_number}
         * @return {*}
         */
        public function roomPeople($area_number, $floor_number, $dorm_floor, $room_number)
        {
            $livePeople = $this->db->get_where('dorm_bed' ,array('area_number' => $area_number, 'floor_number' => $floor_number, 'dorm_floor' => $dorm_floor, 'room_number' => $room_number, 'check_inWhether' => 'X'));
            if($livePeople)
            {
                return json_encode(array('res' => '40000', 'msg' => '查询成功', 'data' => $livePeople->result()));
            }
            else
            {
                return json_encode(array('res' => '40001', 'msg' => '查询无数据', 'data' => ''));
            }
            
        }

        /**
         * @description: 查询系统未住人空房间
         * @param {room_type, room_sex}
         * @return {*}
         */
        public function no_live_room($area_number, $floor_number, $dorm_floor, $room_type, $room_sex)
        {
            //取宿舍实际人数为零的宿舍
            $select = array('area_number', 'area_name', 'floor_number', 'floor_name', 'dorm_floor', 'room_number', 'peopleNumber');
            if($area_number == '' AND $floor_number == '' AND $dorm_floor == '')
            {
                $where = array(
                    'peopleNumber' => '0', 
                    'room_type' => $room_type,
                    'room_sex' => $room_sex
                ); 
            }
            elseif($area_number == '' AND $floor_number == '' AND $dorm_floor <> '')
            {
                $where = array(
                    'peopleNumber' => '0', 
                    'dorm_floor' => $dorm_floor,
                    'room_type' => $room_type,
                    'room_sex' => $room_sex
                ); 
            }
            elseif($area_number == '' AND $floor_number <> '' AND $dorm_floor <> '')
            {
                $where = array(
                    'peopleNumber' => '0', 
                    'floor_number' => $floor_number,
                    'dorm_floor' => $dorm_floor,
                    'room_type' => $room_type,
                    'room_sex' => $room_sex
                ); 
            }
            elseif($area_number <> '' AND $floor_number <> '' AND $dorm_floor <> '')
            {
                $where = array(
                    'peopleNumber' => '0', 
                    'area_number' => $area_number,
                    'floor_number' => $floor_number,
                    'dorm_floor' => $dorm_floor,
                    'room_type' => $room_type,
                    'room_sex' => $room_sex
                );

            }
            elseif($area_number <> '' AND $floor_number == '' AND $dorm_floor == '')
            {
                $where = array(
                    'peopleNumber' => '0', 
                    'area_number' => $area_number,
                    'room_type' => $room_type,
                    'room_sex' => $room_sex
                );
            }
            elseif($area_number <> '' AND $floor_number <>'' AND $dorm_floor == '')
            {
                $where = array(
                    'peopleNumber' => '0', 
                    'area_number' => $area_number,
                    'floor_number' => $floor_number,
                    'room_type' => $room_type,
                    'room_sex' => $room_sex
                );

            }
            $this->db->select($select);
            $this->db->where($where);
            $query = $this->db->get('dorm_roominfor');
            if($query->num_rows()>0)
            {
                $result = json_encode(array('res' => '40000', 'msg' => '查询成功', 'data' => $query->result()));
            }
            else
            {
                $result = json_encode(array('res' => '40001', 'msg' => '查询无数据', 'data' => ''));
            }

            return $result;
        }
}
